package com.homwee.listMenu.util;

import java.util.ArrayList;
import java.util.List;


//import com.homwee.factory.R;
import com.homwee.listMenu.util.MenuItemData.EN_ItemType;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MenuListView extends ListView{
	public static String TAG ="MenuListView";


	public interface onMenuTitleChange{
		public void onMenuTitleChange(MenuItemData ItemData);
	}
	static public int itemEveryPage =10;
	protected MenuItemAdapter mListMenuAdapter =null;
	protected List<MenuItemData> mListItemData =null;
	protected List<MenuItemData> curPageItemData =null;
	private int mCurItemIndex =0;
	private int mCurItemPos =0;
	private Context mContext;
	private onMenuTitleChange mOnTitleChange =null;
	private int mInputNum =0;
	private MenuItemView mCurView = null;
	private boolean hasHide =false;

	public MenuListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext =context;
	}
	public MenuListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext =context;
	}
	public MenuListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		mContext =context;
	}




	@Override
	public void setAdapter(ListAdapter arg0) {
		// TODO Auto-generated method stub
		if(arg0 instanceof MenuItemAdapter){
			mListMenuAdapter =(MenuItemAdapter) arg0;
		}
		super.setAdapter(arg0);
	}
	public void init(List<MenuItemData> ListItemData){
		if(ListItemData ==null || ListItemData.size() ==0){
			return;
		}
		this.mListItemData =ListItemData;
		for(MenuItemData item:mListItemData){
			if(item.isEnable ==true){
				mCurItemIndex =mListItemData.indexOf(item);
				mCurItemPos =mCurItemIndex%itemEveryPage;
				break;
			}
		}
		if(mListMenuAdapter ==null){
			mListMenuAdapter =new MenuItemAdapter(mContext);
		}
		Log.v(TAG,"mCurItemIndex ="+mCurItemIndex);
		Log.v(TAG,"mCurItemPos ="+mCurItemPos);
		
		mListMenuAdapter.init(getCurPageData(mCurItemIndex));
		setAdapter(mListMenuAdapter);

		setSelection(mCurItemPos);
		if(mOnTitleChange !=null){
			mOnTitleChange.onMenuTitleChange(mListItemData.get(0));
		}
	}
	public void init(List<MenuItemData> ListItemData ,int posIndex){
		
		if(ListItemData ==null || ListItemData.size() ==0){
			return;
		}
		this.mListItemData =ListItemData;
		if(posIndex !=-1){
		mCurItemIndex =posIndex;
		}
		mCurItemPos =mCurItemIndex%itemEveryPage;
		if(mListMenuAdapter ==null){
			mListMenuAdapter =new MenuItemAdapter(mContext);
		}
		Log.v("tv","mCurItemIndex ="+mCurItemIndex);
		Log.v("tv","mCurItemPos ="+mCurItemPos);
		
		mListMenuAdapter.init(getCurPageData(mCurItemIndex));
		setAdapter(mListMenuAdapter);
		setSelection(mCurItemPos);
		if(mOnTitleChange !=null){
			mOnTitleChange.onMenuTitleChange(mListItemData.get(0));
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		Log.v("tv","MenuListView onKeyDown");
		mCurView = (MenuItemView) getSelectedView();
		switch (keyCode) {
		case KeyEvent.KEYCODE_0:
		case KeyEvent.KEYCODE_1:		
		case KeyEvent.KEYCODE_2:		
		case KeyEvent.KEYCODE_3:		
		case KeyEvent.KEYCODE_4:		
		case KeyEvent.KEYCODE_5:		
		case KeyEvent.KEYCODE_6:		
		case KeyEvent.KEYCODE_7:		
		case KeyEvent.KEYCODE_8:		
		case KeyEvent.KEYCODE_9:
		case KeyEvent.KEYCODE_DPAD_RIGHT: 
			if(mCurView !=null ){
				mCurView.onKeyDown(keyCode, event);
				if(mCurView.mMenuData.mItemType ==EN_ItemType.EN_HAVESUBCHILD &&
						mCurView.getChildMenuItemData()!=null &&mCurView.mMenuData.mPassword){
					init(mCurView.getChildMenuItemData());
					mListMenuAdapter.notifyDataSetChanged();
					return true;
				}
			}
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			if(mCurView !=null ){
				mCurView.onKeyDown(keyCode, event);
			}
			break;
		case KeyEvent.KEYCODE_DPAD_UP:
			int posPreIndex =getPreItemIndex(mCurItemIndex);
			if(posPreIndex/itemEveryPage !=mCurItemIndex/itemEveryPage){
				mListMenuAdapter.init(getCurPageData(posPreIndex));
				mCurItemPos =posPreIndex%itemEveryPage;
				setSelection(mCurItemPos);
				mListMenuAdapter.notifyDataSetChanged();
				return true;
			}else if(posPreIndex !=mCurItemIndex){
				mCurItemIndex =posPreIndex;
				mCurItemPos =posPreIndex%itemEveryPage;
				setSelection(mCurItemPos);
				return true;
			}
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			int posNextIndex =getNextItemIndex(mCurItemIndex);
			if(posNextIndex/itemEveryPage !=mCurItemIndex/itemEveryPage){
				mListMenuAdapter.init(getCurPageData(posNextIndex));
				mCurItemPos =posNextIndex%itemEveryPage;
				setSelection(mCurItemPos);
				mListMenuAdapter.notifyDataSetChanged();
				return true;
			}else if(posNextIndex !=mCurItemIndex){
				mCurItemIndex =posNextIndex;
				mCurItemPos =posNextIndex%itemEveryPage;
				setSelection(mCurItemPos);
				return true;
			}
			break;
		case KeyEvent.KEYCODE_ENTER:
		case KeyEvent.KEYCODE_DPAD_CENTER:	
			if(mCurView !=null){
				mCurView.mMenuData.onEnterKey();
				if(mCurView.getChildMenuItemData() !=null && mCurView.mMenuData.mPassword){
					init(mCurView.getChildMenuItemData());
					mListMenuAdapter.notifyDataSetChanged();
					return true;
				}
			}
			break;
				
		case KeyEvent.KEYCODE_BACK:
			if(mCurView !=null){
				MenuItemData parent = mListItemData.get(0).mParent;
				if(mListItemData.get(0).mParentGroup !=null){
					mListItemData =mListItemData.get(0).mParentGroup;
					init(mListItemData,mListItemData.indexOf(parent));
					return true;
				}
				else{
					Log.d("zcj", "Arthur1");
					mCurView.onKeyDown(keyCode, event);
				}
			}
			break;
		default:
			break;
		}
		return false;
	}
	private int getPreItemIndex(int CurPos){
		CurPos --;
		while (CurPos >=0) {
			if (mListItemData.get(CurPos).isEnable) {
				break;
			}
			CurPos =CurPos-1;
		}
		if(CurPos<0){
			return mCurItemIndex;
		}
		return CurPos;
	}
	private int getNextItemIndex(int CurPos){
		CurPos ++;
		while (CurPos <mListItemData.size()) {
			if (mListItemData.get(CurPos).isEnable) {
				break;
			}
			CurPos =CurPos+1;
		}
		if(CurPos ==mListItemData.size()){
			CurPos =0;
			do{
				if (mListItemData.get(CurPos).isEnable) {
					break;
				}
				CurPos++;
			}while(CurPos <mListItemData.size()-1);
		}
		return CurPos;
	}
	private List<MenuItemData> getCurPageData(int curIndex){
		if(curPageItemData ==null){
			curPageItemData =new ArrayList<MenuItemData>();
		}
		curPageItemData.clear();
		mCurItemIndex =curIndex;
		int curPage =curIndex/itemEveryPage;
		for(int i=0;i<itemEveryPage;i++){
			int index =i+itemEveryPage*curPage;
			if(index <mListItemData.size()){
				Log.v("tv","index ="+index);	
			curPageItemData.add(mListItemData.get(index));
			}
		}
		return curPageItemData;
	}

	public void setOnMenuTitleChange(onMenuTitleChange func){
		mOnTitleChange =func;
	}
}